import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
@NoArgsConstructor
public class ChatClient extends JFrame {
    private Socket socket;
    private String name;
    private List<ChatClient> clientList=new ArrayList<ChatClient>();
    static List<Socket> socketList=new ArrayList<Socket>();
    //定义集合类对象all存储服务器端返回的socket的输出流对象
    static List<ObjectOutputStream> all = new ArrayList<ObjectOutputStream>();
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private JTextField inputField;
    private JTextArea showArea;
    private JButton btn;
    private String message;

    public void close() {
        try {
            ois.close();
            oos.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void crateUI(List<ChatClient> clientList) {
        this.clientList = clientList;

        //创建一个聊天窗口
        JFrame f = new JFrame("聊天窗口");
        f.setLayout(new BorderLayout());
        f.setSize(400, 300);
        f.setLocation(300, 200);
        f.setVisible(true);
        //创建一个文本域，接收信息
        showArea = new JTextArea(12, 34);
        JScrollPane scrollPane = new JScrollPane(showArea);
        showArea.setEditable(false);   //设置文本域不可编辑
        try {
            //读取服务器返回的信息并添加到文本域  oos.writeObject(name+",您好！欢迎您进入聊天室");
            //ois = new ObjectInputStream(socket.getInputStream());
            String msg = (String) ois.readObject();

            showArea.append(msg + "\n");
            //获取用户名
            int endIndex = msg.indexOf(",");
            name = msg.substring(0, endIndex);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //创建文本框，输入信息
        inputField = new JTextField(20);
        btn = new JButton("发送");
        //按钮监听事件
        btn.addActionListener(e -> {
            message = inputField.getText();
            //System.out.println(name + "说：" + message);
            inputField.setText("");//发送之后，设置聊天框为空
            if (message != null && !message.equals("")) {
                //showArea.append(name + "说:" + message + "\n");
                try {
                    oos.writeObject(message);//将消息发送到服务器

                    /*for (Socket socket : socketList){
                        //new Thread(new ClientRecv(ois)).start();
                        //接收信息
                        if (socket == this.socket){
                            System.out.println("没把发");
                        }else {
                            //ois = new ObjectInputStream(socket.getInputStream());
                            new Thread(new ClientRecv(ois)).start();
                        }
                    }*/
                } catch (IOException ex) {
                    System.out.println("io异常");
                    ex.printStackTrace();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                showArea.append("聊天信息不能为空！！！" + "\n");
            }
            inputField.setText("");
        });

        //接收信息
        for (ChatClient client : clientList){
            new Thread(new ClientRecv(client)).start();
        }
        //new Thread(new ClientRecv(ois)).start();
        /*for (Socket socket : socketList){

            if (socket == this.socket){
                System.out.println("没把发");
            }else {
                //ois = new ObjectInputStream(socket.getInputStream());
                new Thread(new ClientRecv(ois)).start();
            }
        }*/

        //当关闭窗口的时候，退出系统
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        //创建一个面板组件
        JPanel panel = new JPanel();
        JLabel label = new JLabel("聊天信息");
        panel.add(label);
        panel.add(inputField);
        panel.add(btn);

        f.add(scrollPane, BorderLayout.PAGE_START);
        f.add(panel, BorderLayout.PAGE_END);
    }
    public ChatClient(Socket socket, ObjectOutputStream oos) {
        this.socket = socket;
        this.oos = oos;
        try {
            ois = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        socketList.add(socket);
    }

    /*public ChatClient(Socket socket, ObjectOutputStream oos, ObjectInputStream ois) {
        this.socket = socket;
        this.oos = oos;
        this.ois = ois;
        all.add(oos);
        //创建一个聊天窗口
        JFrame f = new JFrame("聊天窗口");
        f.setLayout(new BorderLayout());
        f.setSize(400, 300);
        f.setLocation(300, 200);
        f.setVisible(true);
        //创建一个文本域，接收信息
        showArea = new JTextArea(12, 34);
        JScrollPane scrollPane = new JScrollPane(showArea);
        showArea.setEditable(false);   //设置文本域不可编辑
        try {
            //读取服务器返回的信息并添加到文本域  oos.writeObject(name+",您好！欢迎您进入聊天室");
            //ois = new ObjectInputStream(socket.getInputStream());
            String msg = (String) ois.readObject();

            showArea.append(msg + "\n");
            //获取用户名
            int endIndex = msg.indexOf(",");
            name = msg.substring(0, endIndex);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //创建文本框，输入信息
        inputField = new JTextField(20);
        btn = new JButton("发送");

        //按钮监听事件
        btn.addActionListener(e -> {
            message = inputField.getText();
            System.out.println(name + "说：" + message);
            inputField.setText("");//发送之后，设置聊天框为空
            if (message != null && !message.equals("")) {
                showArea.append(name + "说:" + message + "\n");
                try {
                    oos.writeObject(message);//将消息发送到服务器
                    //接收信息
                    new Thread(new ClientRecv(this)).start();

                } catch (IOException ex) {
                    System.out.println("io异常");
                    ex.printStackTrace();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                showArea.append("聊天信息不能为空！！！" + "\n");
            }
            inputField.setText("");
        });
        //创建一个面板组件
        JPanel panel = new JPanel();
        JLabel label = new JLabel("聊天信息");
        panel.add(label);
        panel.add(inputField);
        panel.add(btn);

        f.add(scrollPane, BorderLayout.PAGE_START);
        f.add(panel, BorderLayout.PAGE_END);

    }*/
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 9900);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            new Login(socket,oos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}